package cn.lingnan.service.serviceimpl;

import cn.lingnan.mapper.UserMapper;
import cn.lingnan.pojo.dto.*;
import cn.lingnan.pojo.entity.*;
import cn.lingnan.pojo.vo.*;
import cn.lingnan.service.UserService;
import cn.lingnan.utils.LargeModelUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageBean getAll(UserPageQueryAllDTO userPageQueryAllDTO) {
        PageHelper.startPage(userPageQueryAllDTO.getPage(),userPageQueryAllDTO.getPageSize());

        Page<Property> page = userMapper.getAll(userPageQueryAllDTO);
        long total = page.getTotal();
        List<Property> list = page.getResult();
        return new PageBean(total,list);
    }

    @Override
    public PageBean getSecondhand(UserPageSecendHandDTO userPageSecendHandDTO) {
        PageHelper.startPage(userPageSecendHandDTO.getPage(),userPageSecendHandDTO.getPageSize());

        Page<Property> page = userMapper.getSecondhand(userPageSecendHandDTO);
        long total = page.getTotal();
        List<Property> list = page.getResult();
        return new PageBean(total,list);
    }

    @Override
    public PropertyVO getPropertyById(Integer propertyId) {
        return  userMapper.getPropertyById(propertyId);
    }

    @Override
    public PageBean getLikeProperty(UserPageLikeDTO userPageLikeDTO) {
        PageHelper.startPage(userPageLikeDTO.getPage(),userPageLikeDTO.getPageSize());
        Page<Property> page = userMapper.getLikeProperty(userPageLikeDTO);
        long total = page.getTotal();
        List<Property> list = page.getResult();
        return new PageBean(total,list);
    }

    @Override
    public Integer saveFavorite(Long userId, Long propertyId) {
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setPropertyId(propertyId);
        favorite.setFavoriteDate(LocalDateTime.now());
      return  userMapper.saveFavorite(favorite);
    }

    @Override
    public Integer saveTransaction(Long userId, Long propertyId, double amount) {
        Transaction transaction = new Transaction();
        transaction.setUserId(userId);
        transaction.setPropertyId(propertyId);
        transaction.setAmount(amount);
        transaction.setTransactionDate(LocalDateTime.now());
        transaction.setStatus(0);
       return userMapper.saveTransaction(transaction);

    }

    @Override
    public List<TransactionVO> getTransactionByUserId(Long userId, Integer type) {
        return userMapper.getTransactionByUserId(userId,type);
    }

    @Override
    public   Integer deleteTransactionById(Long transactionId) {
         return userMapper.deleteTransactionById(transactionId);
    }

    @Override
    public UserVO getUserById(Long userId) {
        return  userMapper.getUserById(userId);
    }

    @Override
    public void updateUserImage(Long userId, String url) {
        userMapper.updateUserImage(userId,url);
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        userDTO.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(userDTO);
    }

    @Override
    public  List<ReviewVO > getReviewByPropertyId(Integer propertyId) {
        return  userMapper.getReviewByPropertyId(propertyId);
    }

    @Override
    public void saveReview(ReviewDTO reviewDTO) {
        Review review = new Review();
        BeanUtils.copyProperties(reviewDTO,review);
        review.setReviewDate(LocalDateTime.now());

        userMapper.saveReview(review);
    }

    @Override
    public List<FavoriteVO> getFavoriteByUserId(Long userId) {
        return userMapper.getFavoriteByUserId(userId);
    }

    @Override
    public void deleteFavoriteById(Long favoriteId) {
        userMapper.deleteFavoriteById(favoriteId);
    }

    @Override
    public void saveMyPostProperty(PropertyDTO propertyDTO) {
        Property property = new Property();
        BeanUtils.copyProperties(propertyDTO,property);
//        property.setCreateTime(LocalDateTime.now());
//        property.setUpdateTime(LocalDateTime.now());
        property.setStatus(1);
        userMapper.saveMyPostProperty(property);
    }

    @Override
    public List<MyPropertyVO> getMyPostProperty(Long userId) {
        return userMapper.getMyPostProperty(userId);
    }

    @Override
    public void StartOrStop(Long propertyId, Integer status) {
        LocalDateTime UpdateTime = LocalDateTime.now();
        userMapper.StartOrStop(propertyId,status,UpdateTime);
    }

    @Override
    public List<BookingVO> getBookingsByUserId(Long userId) {
        return  userMapper.getBookingsByUserId(userId);
    }

    @Override
    public void saveBooking(BookingVO bookingVO) {
        Booking booking = new Booking();
        BeanUtils.copyProperties(bookingVO,booking);
       userMapper.saveBooking(booking);
    }

    @Override
    public void deleteBookingById(Long bookingId) {
        userMapper.deleteBookingById(bookingId);
    }
}
